/**
 * 作者: wgx
 * 日期: 2024-10-25 09:51:45
 * 备注: 用户城镇摆放坐标数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCityCoord;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserCityCoordModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.crood";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // user_id
    protected long crood; // 坐标 (1zzxxxxyyyy)
    protected int itemId; // 建筑id或是装饰物id
    protected int isTurn; // 是否翻转（0不翻转 1翻转）
    protected String param = ""; // 特殊配置
    protected long buildTime; // 首次建造时间,移动时不改变

    public void reset() {
    }

    protected String getKey() {
        return "UserCityCoord." + userId + "." + crood;
    }

    protected String getListKey() {
        return "UserCityCoord." + userId;
    }

    protected String getListItemKey() {
        return crood + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_city_coord"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // user_id
        obj.put("crood", crood); // 坐标 (1zzxxxxyyyy)
        obj.put("itemId", itemId); // 建筑id或是装饰物id
        obj.put("isTurn", isTurn); // 是否翻转（0不翻转 1翻转）
        obj.put("param", param); // 特殊配置
        obj.put("buildTime", buildTime); // 首次建造时间,移动时不改变
        return obj;
    }

    public PBUserCityCoord.Builder takeInitBuilder() {
        PBUserCityCoord.Builder sb = PBUserCityCoord.newBuilder();
        if (itemId != 0) {
            sb.setItemId(itemId);
        }
        if (isTurn != 0) {
            sb.setIsTurn(isTurn);
        }
        if (!param.isEmpty()) {
            sb.setParam(param);
        }
        if (buildTime != 0) {
            sb.setBuildTime(buildTime);
        }
        return sb;
    }

    public PBUserCityCoord.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserCityCoord.Builder sb = PBUserCityCoord.newBuilder();
        if (isFieldUpdate("itemId")) {
            sb.setItemId(itemId);
        }
        if (isFieldUpdate("isTurn")) {
            sb.setIsTurn(isTurn);
        }
        if (isFieldUpdate("param")) {
            sb.setParam(param);
        }
        if (isFieldUpdate("buildTime")) {
            sb.setBuildTime(buildTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getCrood() {
        return crood;
    }

    public void putCrood(long crood) {
        if (this.crood != crood) {
            this.crood = crood;
            updateRedisHashField(getKey(), "crood", this.crood);
        }
    }

    public void addCrood(long value) {
        this.crood += value;
        updateRedisHashField(getKey(), "crood", this.crood);
    }

    public int getItemId() {
        return itemId;
    }

    public void putItemId(int itemId) {
        if (this.itemId != itemId) {
            this.itemId = itemId;
            updateRedisHashField(getKey(), "itemId", this.itemId);
        }
    }

    public void addItemId(int value) {
        this.itemId += value;
        updateRedisHashField(getKey(), "itemId", this.itemId);
    }

    public int getIsTurn() {
        return isTurn;
    }

    public void putIsTurn(int isTurn) {
        if (this.isTurn != isTurn) {
            this.isTurn = isTurn;
            updateRedisHashField(getKey(), "isTurn", this.isTurn);
        }
    }

    public void addIsTurn(int value) {
        this.isTurn += value;
        updateRedisHashField(getKey(), "isTurn", this.isTurn);
    }

    public String getParam() {
        return param;
    }

    public void putParam(String param) {
        if (!this.param.equals(param)) {
            this.param = param;
            updateRedisHashField(getKey(), "param", this.param);
        }
    }

    public long getBuildTime() {
        return buildTime;
    }

    public void putBuildTime(long buildTime) {
        if (this.buildTime != buildTime) {
            this.buildTime = buildTime;
            updateRedisHashField(getKey(), "buildTime", this.buildTime);
        }
    }

    public void addBuildTime(long value) {
        this.buildTime += value;
        updateRedisHashField(getKey(), "buildTime", this.buildTime);
    }

}